{% extends "../layouts/admin.html" %} {% block content %}

<header class="header navbar bg-white shadow">
	<div class="btn-group tool-button">
		<a class="btn btn-primary navbar-btn" href="/system/setup/add" data-pjax><i class="ti-plus"></i> 添加配置项</a>
	</div>
</header>
<div class=panel-body style="padding-top: 50px;">
	<div class="table-responsive no-border">
		<input id="unitid" type="hidden">
		<table class="table table-bordered table-striped mg-t datatable">
			<thead>
				<tr>
					<th>配置项</th>
					<th>名称</th>
					<th>类型</th>
					<th>分组</th>
					<th>参数值</th>
					<th>操作</th>
		</table>
	</div>
</div>
<div id="dialogDelete" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title">删除</h4>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-xs-12">
						删除后无法恢复，确定删除吗？ <br/>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
				<button id="ok" type="button" class="btn btn-primary" data-loading-text="正在删除...">确 定</button>
			</div>
		</div>
	</div>
</div>
<script language="JavaScript">
	var datatable;

	function initDatatable() {
		datatable = $('.datatable').DataTable({
			"dom": '<"toolbar">frtip',
			"searching": false,
			"processing": false,
			"serverSide": true,
			"select": true,
			"ordering": true,
			"language": {
				"url": "/assets/plugins/datatables/cn.json"
			},
			"preDrawCallback": function() {
				sublime.showLoadingbar($(".main-content"));
			},
			"drawCallback": function() {
				sublime.closeLoadingbar($(".main-content"));
			},
			"ajax": {
				"url": "/system/setup/index",
				"type": "post",
				"data": function(d) {}
			},
			"order": [
				[0, "asc"]
			],
			"columns": [{
					"data": "code",
					"bSortable": true
				},
				{
					"data": "name",
					"bSortable": true
				},
				{
					"data": "type",
					"bSortable": true
				},
				{
					"data": "group",
					"bSortable": true
				},
				{
					"data": "value",
					"bSortable": true
				}
			],
			"columnDefs": [{
					"render": function(data, type, row) {
						let setupGroup = {{config.setup.setupGroup | stringify | safe }};

						return setupGroup[row.group];
					},
					"targets": 3
				},
				{
					"render": function(data, type, row) {
						let setupType = {{	config.setup.setupType | stringify | safe	}};

						return setupType[row.type];
					},
					"targets": 2
				},
				{
					"render": function(data, type, row) {
						var s = '<li><a href="javascript:;" onclick="del(' + row.id + ')">删除</a></li>';
						//          if(row.code.indexOf('system.')>-1){
						//            s='';
						//          }
						return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
							' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
							'<li><a href="/system/setup/edit?id=' + row.id + '" data-pjax>修改</a></li>' +
							s +
							'</ul></div>';
					},
					"targets": 5
				}
			]
		});
		datatable.on('click', 'tr', function() {
			$(this).toggleClass('selected');
		});
		$("#searchBtn").on('click', function() {
			datatable.ajax.reload();
		});
	}

	function del(id) {
		var dialog = $("#dialogDelete");
		dialog.modal("show");
		dialog.find("#ok").unbind("click");
		dialog.find("#ok").bind("click", function(event) {
			var btn = $(this);
			btn.button("loading");
			$.post("/system/setup/delete", {id:id}, function(data) {
				if(data.errno == 0) {
					datatable.ajax.reload(null, false);
				} else {
					Toast.error(data.errmsg);
				}
				//重置按钮状态，关闭提示框
				btn.button("reset");
				dialog.modal("hide");
			}, "json");
		});
	}
	$(function() {
		initDatatable();
	});
</script>
{% endblock %}